<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="doAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <!-- :default-expand-all="true" 默认展开所有节点 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.manager }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.name }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="doAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>

        <!-- 弹层组件 -->
        <el-dialog
          title="添加或编辑"
          :visible.sync="dialogVisible"
          :close-on-press-escape="false"
          :close-on-click-modal="false"
          width="30%"
        >
          <!-- 父传子将父级部门ID传给子组件 -->
          <AddorEdit
            v-if="dialogVisible"
            :id="curId"
            :is-edit="isEdit"
            @success="hSuccess"
            @close="dialogVisible = false"
          />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/indexU'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      list: [],
      dialogVisible: false, // 控制弹层组件是否显示
      curId: '', // 父级部门ID
      isEdit: false // 用来判断用户是添加还是编辑，false添加，true是编辑

    }
  },
  created() {
    this.lodeTree()
  },
  methods: {
    // 获取部门列表
    async lodeTree() {
      try {
        const res = await getDepartments()
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },

    // 添加子部门
    doAdd(pid) {
      this.dialogVisible = true
      // 打开弹层时将父级部门ID赋值给curId
      this.curId = pid
      // 将isEdit传递给子组件用来判断是编辑还是添加
      this.isEdit = false
    },

    // 编辑
    hEdit(pid) {
      this.dialogVisible = true
      // 打开弹层时将父级部门ID赋值给curId
      this.curId = pid
      // 将isEdit传递给子组件用来判断是编辑还是添加
      this.isEdit = true
    },

    // 删除
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delDepartment(id)
          console.log(res)
          this.lodeTree()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {

      })
    },

    hSuccess() {
      // 添加成功后关闭弹窗
      this.dialogVisible = false
      // 重新获取部门信息
      this.lodeTree()
    }
  }
}
</script>
